`include "PRV564Config.v"
`include "PRV564Define.v"
module PC(
    input  wire            CLKi,
    input  wire            ARSTi,
    input  wire [`XLEN-1:0]B_newPC,         //当进行BP时使用的PC
    input  wire [`XLEN-1:0]G_newPC,         //当进行流水线冲刷时使用的PC
    input  wire            B_flush,         //分支使用的刷新
    input  wire            G_flush,         //全局使用的刷新
    input  wire            BTB_predicted,   //分支预测为跳转
    input  wire [`XLEN-1:0]BTB_predictedPC, //分支预测的下一个pc
    output  reg [`XLEN-1:0]PC_Val,
    output  reg            PC_valid,        //Current PC is valid
    output wire [`XLEN-1:0]PCnext_predicted,//预测的下一个PC
    input  wire            Ready            //后级准备好
);
always@(posedge CLKi or posedge ARSTi)begin
    if(ARSTi)begin
        PC_Val <= `PC_reset;
    end
    else if(G_flush)begin
        PC_Val <= G_newPC;
    end
    else if(B_flush)begin
        PC_Val <= B_newPC;
    end
    else if(Ready)begin
        PC_Val <= PCnext_predicted;
    end
end
always@(posedge CLKi or posedge ARSTi)begin
    if(ARSTi)begin
        PC_valid <= 1'b0;
    end
    else begin
        PC_valid <= 1'b1;
    end
end
assign PCnext_predicted = BTB_predicted ? BTB_predictedPC : (PC_Val+64'd4); //如果分支预测起效，那PCnext是BTB中的预测值
endmodule

